<template>
    <div class="shopcar-container">
        <div class="goods-list">

            <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner" v-for="(item,i) in goodList" :key="item.id">
						<mt-switch v-model="$store.getters.getGoodsselected[item.id]"
                        @change="selectedChange(item.id,$store.getters.getGoodsselected[item.id])"
                        
                        ></mt-switch>
                        <img :src="item.thumb_path" >
                        <div class="info">
                            <h3>{{item.title}}</h3>
                            <p>
                                <span class="price">￥{{item.sell_price}}</span>
                                <numbox :initcount="$store.getters.getGoodscount[item.id]" :goodsid ="item.id"></numbox>
                                <a href="#" @click.prevent ="remove(item.id,i)">删除</a>
                            </p>
                        </div>
					</div>
				</div>
			</div>

        </div>

             <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner jiesuan">
						<div class="left">
                            <p>总价（不计运费）</p>
                            <p>已勾选商品 <span class="red">{{$store.getters.getcountandamount.count}}</span> 件， 
                            总价 <span class="red">￥{{$store.getters.getcountandamount.amount}}</span></p>
                        </div>
                        <mt-button type="danger">去结算</mt-button>
					</div>
				</div>
			</div>
            
        
    </div>
</template>

<script>
import numbox from '../subcomponents/shopcar_numbox.vue'
export default {
    
    data(){
        return{
            goodList:[]
        }
    },
    created(){
        this.getGoodsList();
    },
    methods:{
        getGoodsList(){
            //1.获取商品中的ID，拼接成一个字符串
            var idArr=[];
            this.$store.state.car.forEach(item=>idArr.push(item.id));   // 先从 store 里面循环遍历一下，把遍历出来的id push 到 idArr里面。

            if(idArr.length<=0){   //如果Id为空直接返回，不然报错。
                return;
            }

            this.$http.get('http://www.liulongbin.top:3005/api/goods/getshopcarlist/'+ idArr.join(',')).then(result=>{
                if(result.body.status === 0){
                    this.goodList = result.body.message;
                }
            })
        },
        remove(id,index){
            this.goodList.splice(index,1);
            this.$store.commit('removeFromcar',id)
        },
        selectedChange(id,val){
            console.log(id,val)
            this.$store.commit('updateGoodsSelected',{id,selected:val})
        }
    },
    components:{
        numbox
    },
}
</script>

<style scoped>
.shopcar-container{
    background-color: #eee;
    overflow: hidden;

}
.shopcar-container .goods-list img{
    width: 60px;
    height: 60px;
    
}
.shopcar-container .goods-list h3{
    font-size: 13px;

}
.shopcar-container .goods-list .price{
    color: red;
    font-weight: bold;
}
.shopcar-container .goods-list .mui-card-content-inner{
    display: flex;
    align-items: center;
}
.jiesuan{
    display: flex;
    justify-content:space-between;
    align-items: center;
}
.jiesuan .red{
    color: red;
    font-weight: bold;
    font-size: 16px;
}
    
</style>
